package com.whitlock.backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whitlock.backend.dao.UserDao;
import com.whitlock.backend.entity.TKUser;
import com.whitlock.backend.exception.BusinessServiceException;
import com.whitlock.backend.jwt.JwtUtil;
import com.whitlock.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 客户端用户表 服务实现类
 * </p>
 *
 *  * @author Whitlock
 *  * @date 2020/7/9   10:39 上午
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, TKUser> implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @Override
    public TKUser login(TKUser user) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_name", user.getUserName());
        map.put("user_password", user.getUserPassword());
        TKUser user1 = null;
        try {
            user1 = userDao.selectByMap(map).get(0);
            //根据用户名和密码和用户id加密生成token
            String token = JwtUtil.sign(user1.getUserName(), user1.getUserPassword(),user1.getId());
            user1.setToken(token);
            return user1;
        } catch (Exception e) {
            throw new BusinessServiceException("用户名或密码错误");
        }
    }
    //删除用户
    @Override
    public void deleteUser(String id){
        userDao.deleteById(id);
    }

    //更新用户
    @Override
    public void updateUser(TKUser user){
        userDao.updateById(user);
    }

    //新增用户
    @Override
    public void addUser(TKUser user){
        userDao.insert(user);
    }

    //查询用户列表

}
